﻿using System;
using System.Collections.Specialized;
using System.Drawing;
using System.Windows.Forms;
using __ts.Core.Configuration;

namespace CCU.Controls
{
    internal partial class ConfigDataGridView : UserControl
    {
        private int port = 0;
        private DataGridViewRow currentRow = null;

        public ConfigDataGridView()
        {
            InitializeComponent();
        }

        public void Init()
        {
            this.btnAddOrEdit.Visible = false;
            this.btnCancel.Visible = false;
            this.middlePanel.Visible = false;

            NameValueCollection configs = ConfigManager.GlobalAppSettings;
            foreach (string name in configs.AllKeys)
            {
                this.dataGridView.Rows.Add(name, configs[name]);
            }
        }

        public void Init(int port)
        {
            this.port = port;

            this.btnAddOrEdit.Visible = false;
            this.btnCancel.Visible = false;
            this.middlePanel.Visible = false;

            NameValueCollection configs = ConfigManager.AppSettings[port];
            foreach (string name in configs.AllKeys)
            {
                this.dataGridView.Rows.Add(name, configs[name]);
            }
        }

        #region button events

        private void btnAddOrEdit_Click(object sender, EventArgs e)
        {
            Button button = sender as Button;
            NameValueCollection configs = ConfigManager.AppSettings[port];

            if (button.Text == "Add")
            {
                //if (TextBoxRequired.IsAllTextBoxRequiredSet(this.middlePanel))
                //{
                //    string name = txtName.Text.Trim();
                //    string value = txtValue.Text.Trim();
                //    string error;
                //    if (!ModuleServer.ModuleHost.PortConfigManager.IsConfigItemValid(name, value, out error))
                //    {
                //        MessageBox.Show(error);
                //        return;
                //    }
                //    foreach (string key in configs.Keys)
                //    {
                //        if (key == name)
                //        {
                //            MessageBox.Show("The name \"" + name + "\" already exist.");
                //            return;
                //        }
                //    }
                //    this.dataGridView.Rows.Add(name, value);

                //    // save config
                //    configs.Add(name, value);
                //    //ModuleServer.ModuleHost.PortConfigManager.SaveConfig(port, configs);

                //    // hide panel
                //    this.btnCancel_Click(this.btnCancel, e);
                //}
            }
            else if (button.Text == "Edit")
            {
                //if (TextBoxRequired.IsAllTextBoxRequiredSet(this.middlePanel))
                //{
                //    string name = txtName.Text.Trim();
                //    string value = txtValue.Text.Trim();
                //    string error;
                //    if (!ModuleServer.ModuleHost.PortConfigManager.IsConfigItemValid(name, value, out error))
                //    {
                //        MessageBox.Show(error);
                //        return;
                //    }
                //    this.currentRow.Cells[0].Value = name;
                //    this.currentRow.Cells[1].Value = value;

                //    // save config
                //    configs[name] = value;
                //    //ModuleServer.ModuleHost.PortConfigManager.SaveConfig(port, configs);

                //    // hide panel
                //    this.btnCancel_Click(this.btnCancel, e);
                //}
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.currentRow = null;
            this.btnAddOrEdit.Visible = false;
            this.btnCancel.Visible = false;
            this.middlePanel.Visible = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.ParentForm.Close();
        }

        #endregion

        #region dataGridView events

        private DataGridViewRow GetDataGridViewRowByPoint(Point point)
        {
            for (int i = 0; i < this.dataGridView.Rows.Count; i++)
            {
                Rectangle rectangle = this.dataGridView.GetRowDisplayRectangle(i, false);
                if (rectangle.Contains(point))
                {
                    return this.dataGridView.Rows[i];
                }
            }

            return null;
        }

        private void dataGridView_MouseDown(object sender, MouseEventArgs e)
        {
            return;

            DataGridViewRow row = GetDataGridViewRowByPoint(new Point(e.X, e.Y));
            bool rowClicked = (row != null);

            if (row != null)
            {
                row.Selected = true;
            }
            this.editToolStripMenuItem.Enabled = rowClicked;
            this.removeToolStripMenuItem.Enabled = rowClicked;
            this.copyNameToolStripMenuItem.Enabled = rowClicked;
            this.copyValueToolStripMenuItem.Enabled = rowClicked;
        }

        private void dataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            return;

            if (e.RowIndex > -1)
            {
                this.contextMenuStrip_Click(this.editToolStripMenuItem, EventArgs.Empty);
            }
        }

        #endregion

        #region contextMenuStrip events

        private void contextMenuStrip_Click(object sender, EventArgs e)
        {
            if (sender == this.addToolStripMenuItem)
            {
                this.btnAddOrEdit.Text = "Add";
                this.btnAddOrEdit.Visible = true;
                this.btnCancel.Visible = true;
                this.middlePanel.Visible = true;

                this.currentRow = null;
                this.txtName.ReadOnly = false;
                this.txtName.Text = string.Empty;
                this.txtValue.Text = string.Empty;
            }
            else if (sender == this.editToolStripMenuItem)
            {
                this.btnAddOrEdit.Text = "Edit";
                this.btnAddOrEdit.Visible = true;
                this.btnCancel.Visible = true;
                this.middlePanel.Visible = true;

                if (this.dataGridView.SelectedRows.Count > 0)
                {
                    currentRow = this.dataGridView.SelectedRows[0];
                    this.txtName.ReadOnly = true;
                    this.txtName.Text = currentRow.Cells[0].Value.ToString();
                    this.txtValue.Text = currentRow.Cells[1].Value.ToString();
                }
            }
            else if (sender == this.removeToolStripMenuItem)
            {
                if (this.dataGridView.SelectedRows.Count > 0)
                {
                    var row = this.dataGridView.SelectedRows[0];
                    string name = row.Cells[0].Value.ToString();

                    if (MessageBox.Show("Are you sure to remove config node \"" + name + "\"?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        if (currentRow != null && currentRow.Cells[0].Value.ToString() == name)
                        {
                            this.btnCancel_Click(this.btnCancel, e);
                        }
                        this.dataGridView.Rows.Remove(row);

                        // save config
                        NameValueCollection configs = ConfigManager.AppSettings[port];
                        configs.Remove(name);
                        //ModuleServer.ModuleHost.PortConfigManager.SaveConfig(port, configs);
                    }
                }
            }
            else if (sender == this.copyNameToolStripMenuItem)
            {
                if (this.dataGridView.SelectedRows.Count > 0)
                {
                    Clipboard.SetDataObject(this.dataGridView.SelectedRows[0].Cells[0].Value.ToString());
                    MessageBox.Show("The name is copied to the clipboard.");
                }
            }
            else if (sender == this.copyValueToolStripMenuItem)
            {
                if (this.dataGridView.SelectedRows.Count > 0)
                {
                    Clipboard.SetDataObject(this.dataGridView.SelectedRows[0].Cells[1].Value.ToString());
                    MessageBox.Show("The value is copied to the clipboard.");
                }
            }
        }

        #endregion
    }
}
